package com.yiren.dbaa.module.practice_set.site.view.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.module.practice_set.site.contract.SiteRuleContract;
import com.yiren.dbaa.module.practice_set.site.presenter.SiteRulePresenter;
import com.yiren.dbaa.module.practice_set.site.view.activity.AddRuleActivity;
import com.yiren.dbaa.module.practice_set.site.view.activity.SiteManageActivity;
import com.yiren.dbaa.module.practice_set.site.view.adapter.SiteRuleAdapter;
import com.yiren.dbaa.module.project_edit.view.activity.ProjectManageActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/9/20 10:18
 * @description: 考场规则fragment
 */
public class SiteRuleFragment extends LazyLoadFragment implements SiteRuleContract.View {

    @BindView(R.id.lv_site_rule)
    ListView lvSiteRule;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.ll_new_rule)
    LinearLayout llNewRule;

    private SiteRuleContract.Presenter siteRulePresenter;
    private boolean isPartner;//是否是合伙人视角

    @Override
    protected int setContentView() {
        return R.layout.fragment_site_rule;
    }

    @Override
    protected void init() {
        isPartner = LoadConfigApi.loadUserParamConfig(getActivity()).isPartner();
        siteRulePresenter = new SiteRulePresenter(getActivity());
        siteRulePresenter.loadSiteRuleList(this);
        if (isPartner) {
            llNewRule.setVisibility(View.VISIBLE);
        } else {
            llNewRule.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        siteRulePresenter.loadSiteRuleList(this);
    }

    @Override
    public void showSiteRuleList(List<SiteEntity> siteList) {
        if (siteList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            tvTips.setText("您当前考场规则列表为空，请先新建考场规则!");
            lvSiteRule.setVisibility(View.GONE);
        } else {
            llNoData.setVisibility(View.GONE);
            lvSiteRule.setVisibility(View.VISIBLE);
            SiteRuleAdapter siteRuleAdapter = new SiteRuleAdapter(getActivity(), siteList, 0);
            lvSiteRule.setAdapter(siteRuleAdapter);
            //删除
            siteRuleAdapter.setOnDeleteClickListener(position -> {
                CommonDialog.showTwoBtn(getActivity(), "操作确认", "确定删除" + siteList.get(position).getSiteName() + "吗？", "取消", "确认", (dialog, which) -> {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        siteRulePresenter.deleteSiteRule(siteList.get(position), new CommonCallBack() {
                            @Override
                            public void success(Object result) {
                                refreshSiteRuleList();
                                dialog.dismiss();
                            }
                        });
                    }
                });
            });

            //管理下载编辑规则
            siteRuleAdapter.setOnManageClickListener(position -> {
                if (isPartner) {
                    if (siteList.get(position).isDownLoad()) {
                        //管理页
                        Intent intent = new Intent(getActivity(), SiteManageActivity.class);
                        intent.putExtra("site", siteList.get(position));
                        startActivity(intent);
                    } else {
                        //下载规则
                        siteRulePresenter.downLoadSiteRuleData(siteList.get(position), SiteRuleFragment.this);
                    }
                } else {
                    if (siteList.get(position).isDownLoad()) {
                        //编辑规则
                        Intent intent = new Intent(getActivity(), ProjectManageActivity.class);
                        intent.putExtra("site", siteList.get(position));
                        startActivity(intent);
                    } else {
                        //下载规则
                        siteRulePresenter.downLoadSiteRuleData(siteList.get(position), SiteRuleFragment.this);
                    }
                }
            });
            //更新
            siteRuleAdapter.setOnUpdateClickListener(position -> {
                String message;
                if (siteList.get(position).getUpdateDetails() == null) {
                    message = "";
                } else {
                    message = siteList.get(position).getUpdateDetails();
                }
                CommonDialog.showTwoBtn(getActivity(), "更新详情", message, "取消", "立即更新", (dialog, which) -> {
                    dialog.dismiss();
                    if (which != -2) {
                        siteRulePresenter.updateSiteRule(siteList.get(position), SiteRuleFragment.this);
                    }
                });
            });
        }
    }

    @Override
    public void refreshSiteRuleList() {
        siteRulePresenter.loadSiteRuleList(this);
    }

    @OnClick({R.id.ll_new_rule})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_new_rule:
                startActivity(new Intent(getActivity(), AddRuleActivity.class));
                break;
        }
    }
}
